package com.hwh.lib.view.itemDecoration

import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.RecyclerView


/**
 * 文件描述：
 * 作者：hwh
 * 创建时间：2021/6/19
 *
 */
class LinearItemDecoration : RecyclerView.ItemDecoration() {
    companion object {
        private val TAG = "hwh"
    }

    private val paint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)
    private val dividerPaint: Paint = Paint(Paint.ANTI_ALIAS_FLAG)

    init {
        paint.run {
            color = Color.BLUE
            style = Paint.Style.STROKE
            strokeWidth = 5f
        }
        dividerPaint.run {
            color = Color.parseColor("#e6e6e6")
            style = Paint.Style.FILL
        }
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        super.getItemOffsets(outRect, view, parent, state)
        outRect.run {
//            bottom = 10
//            left = 100
//            right = 40
            bottom = 5
            left = 100
        }
    }

    override fun onDraw(canvas: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(canvas, parent, state)

        Log.e(TAG, "onDraw: ")
        val layoutManager = parent.layoutManager
        for (i in 0 until parent.childCount) {
            val childView = parent.getChildAt(i);
            val left = layoutManager!!.getLeftDecorationWidth(childView) / 2
            canvas.drawCircle(
                left.toFloat(),
                (childView.top + childView.height / 2).toFloat(),
                20f,
                paint
            )
            canvas.drawRect(
                Rect(
                    layoutManager.getLeftDecorationWidth(childView),
                    childView.bottom,
                    childView.width + layoutManager.getLeftDecorationWidth(childView),
                    childView.bottom + layoutManager.getBottomDecorationHeight(childView)

                ), dividerPaint
            )
        }

    }
}